<template>
  <div v-if="show" class="previewImage" @tap="close">
    <div v-if="urls.length > 0" class="goback">
      <uni-icons type="back" color="#FFFFFF" size="28" />
    </div>
    <swiper class="swiper" :current="current" @change="swiperChange" @touchstart="handleTouchStart" @touchend="handleTouchEnd">
      <swiper-item v-for="(item, index) in urls" :key="index">
        <movable-area class="movable-area" scale-area>
          <movable-view class="movable-view"
                        direction="all" :inertia="true" damping="100" scale="true"
                        scale-min="1" scale-max="4" :scale-value="scale">
            <scroll-view scroll-y="true" class="uni-scroll-view">
              <div class="scroll-view">
                <image :key="index" class="image"
                       :src="item" mode="widthFix" @longpress="onLongpress(item)" /></div>
            </scroll-view>
          </movable-view>
        </movable-area>
      </swiper-item>
    </swiper>
    <div v-if="urls.length > 0" class="page">
      <div class="w-full flex justify-between">
        <text class="text">{{ current + 1 }} / {{ urls.length }}</text>
        <div v-if="isShowDownLoad" class="text" @click.stop="downLoad">
          <image
            :src="`${cdnUrl}/svgicons/save.svg${imgVersion}`"
            mode="scaleToFill"
            class="h-46rpx w-46rpx"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  props: {
    urls: {
      type: Array,
      required: true,
      default: () => {
        return []
      },
    },
    currentIndex: {
      type: Number,
      required: true,
      default: () => {
        return 0
      },
    },
    isShowDownLoad: {
      type: Boolean,
      required: false,
      default: true,
    },
  },
  data() {
    return {
      show: true,
      current: 0, //当前页
      scale: 1,
      isZooming: false, // 是否处于缩放状态
    }
  },
  created() {
    this.current = this.currentIndex
  },
  methods: {
    //打开
    open(current) {
      this.current = this.urls.findIndex(item => item === current)
      this.show = true
    },
    //关闭
    close() {
      if (!this.isZooming) {
        this.show = false
        this.current = 0
        this.$emit('close')
      }
    },
    //图片改变
    swiperChange(e) {
      this.current = e.detail.current
    },
    //下载图片
    downLoad(item) {
      this.$emit('downLoad', this.urls[this.current])
    },
    //监听长按
    onLongpress(e) {
      this.$emit('downLoad', this.urls[this.current])
    },
    handleTouchStart() {
      this.isZooming = true
    },
    handleTouchEnd() {
      this.isZooming = false
    },
  },
}
</script>

<style lang="scss" scoped>
.previewImage {
  z-index: 9999;
  position: fixed;
  top: 0;
  left: 0;
  width: 92%;
  height: 100%;
  background-color: rgb(0 0 0 / 70%);
  padding:4%;

  .swiper {
    width: 100%;
    height: 100vh;

    swiper-item {
      .movable-area {
        height: 100%;
        width: 100%;

        .movable-view {
          width: 100%;
          min-height: 100%;

          .scroll-view {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;

            .image {
              width: 100%;
              height: auto;
              border-radius: 30rpx;
            }
          }
        }
      }
    }
  }

  .goback{
    position: absolute;
    z-index: 9999;
    width: 100rpx;
    top:100rpx;
    left:0rpx;
    text-align: center;
    color: #fff;
    font-size: 32rpx;
    padding: 3rpx 16rpx;
    border-radius: 20rpx;
    user-select: none;
  }

  .page {
    position: absolute;
    z-index: 9999;
    width: 90%;
    bottom: 160rpx;
    text-align: center;
    display: flex;

    .text {
      color: #fff;
      font-size: 32rpx;
      padding: 3rpx 16rpx;
      border-radius: 20rpx;
      user-select: none;
    }
  }
}
</style>
